import { defineStore } from "pinia";
import VtOptionsAPI from "@/api/vt/vt-options.js";

export const useVtOptionsStore = defineStore("vtOptions", {
  state: () => ({
    // 缓存各种选项数据
    bagTypeOptions: {
      data: [],
      loaded: false,
      loading: false,
    },
    compositeTypeOptions: {
      data: [],
      loaded: false,
      loading: false,
    },
    materialOptions: {
      data: [],
      loaded: false,
      loading: false,
    },
    sizeOptions: {
      data: [],
      loaded: false,
      loading: false,
    },
    surfaceFeeOptions: {
      data: [],
      loaded: false,
      loading: false,
    },
    accessoryOptions: {
      data: [],
      loaded: false,
      loading: false,
    },
    rollOptions: {
      data: [],
      loaded: false,
      loading: false,
    },
    // 按类型缓存的袋型数据
    bagTypeByType: {
      0: { data: [], loaded: false, loading: false }, // 机印
      1: { data: [], loaded: false, loading: false }, // 现货数码印
      2: { data: [], loaded: false, loading: false }, // 其他类型
    },
  }),

  actions: {
    /**
     * 获取袋型选项数据
     * @param {number} type - 类型过滤，0=机印，1=现货数码印
     */
    async getBagTypeOptions(type) {
      // 如果有类型过滤，使用分类缓存
      if (type !== undefined && type !== null) {
        // 确保缓存对象存在
        if (!this.bagTypeByType[type]) {
          this.bagTypeByType[type] = { data: [], loaded: false, loading: false };
        }

        const cache = this.bagTypeByType[type];
        if (cache.loaded) {
          return cache.data;
        }
        if (cache.loading) {
          // 如果正在加载，等待加载完成
          while (cache.loading) {
            await new Promise((resolve) => setTimeout(resolve, 100));
          }
          return cache.data;
        }

        cache.loading = true;
        try {
          const data = await VtOptionsAPI.getBagTypeOptions(type);
          cache.data = data;
          cache.loaded = true;
          return data;
        } finally {
          cache.loading = false;
        }
      }

      // 无类型过滤，使用全部数据缓存
      const cache = this.bagTypeOptions;
      if (cache.loaded) {
        return cache.data;
      }
      if (cache.loading) {
        while (cache.loading) {
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        return cache.data;
      }

      cache.loading = true;
      try {
        const data = await VtOptionsAPI.getBagTypeOptions();
        cache.data = data;
        cache.loaded = true;
        return data;
      } finally {
        cache.loading = false;
      }
    },

    /**
     * 获取复合工艺选项数据
     */
    async getCompositeTypeOptions() {
      const cache = this.compositeTypeOptions;
      if (cache.loaded) {
        return cache.data;
      }
      if (cache.loading) {
        while (cache.loading) {
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        return cache.data;
      }

      cache.loading = true;
      try {
        const data = await VtOptionsAPI.getCompositeTypeOptions();
        cache.data = data;
        cache.loaded = true;
        return data;
      } finally {
        cache.loading = false;
      }
    },

    /**
     * 获取材质选项数据
     */
    async getMaterialOptions() {
      const cache = this.materialOptions;
      if (cache.loaded) {
        return cache.data;
      }
      if (cache.loading) {
        while (cache.loading) {
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        return cache.data;
      }

      cache.loading = true;
      try {
        const data = await VtOptionsAPI.getMaterialOptions();
        cache.data = data;
        cache.loaded = true;
        return data;
      } finally {
        cache.loading = false;
      }
    },

    /**
     * 获取尺寸选项数据
     */
    async getSizeOptions() {
      const cache = this.sizeOptions;
      if (cache.loaded) {
        return cache.data;
      }
      if (cache.loading) {
        while (cache.loading) {
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        return cache.data;
      }

      cache.loading = true;
      try {
        const data = await VtOptionsAPI.getSizeOptions();
        cache.data = data;
        cache.loaded = true;
        return data;
      } finally {
        cache.loading = false;
      }
    },

    /**
     * 获取印刷颜色选项数据
     */
    async getSurfaceFeeOptions() {
      const cache = this.surfaceFeeOptions;
      if (cache.loaded) {
        return cache.data;
      }
      if (cache.loading) {
        while (cache.loading) {
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        return cache.data;
      }

      cache.loading = true;
      try {
        const data = await VtOptionsAPI.getSurfaceFeeOptions();
        cache.data = data;
        cache.loaded = true;
        return data;
      } finally {
        cache.loading = false;
      }
    },

    /**
     * 获取配件选项数据
     */
    async getAccessoryOptions() {
      const cache = this.accessoryOptions;
      if (cache.loaded) {
        return cache.data;
      }
      if (cache.loading) {
        while (cache.loading) {
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        return cache.data;
      }

      cache.loading = true;
      try {
        const data = await VtOptionsAPI.getAccessoryOptions();
        cache.data = data;
        cache.loaded = true;
        return data;
      } finally {
        cache.loading = false;
      }
    },

    /**
     * 获取滚筒选项数据
     */
    async getRollOptions() {
      const cache = this.rollOptions;
      if (cache.loaded) {
        return cache.data;
      }
      if (cache.loading) {
        while (cache.loading) {
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        return cache.data;
      }

      cache.loading = true;
      try {
        const data = await VtOptionsAPI.getRollOptions();
        cache.data = data;
        cache.loaded = true;
        return data;
      } finally {
        cache.loading = false;
      }
    },

    /**
     * 清除所有缓存
     */
    clearCache() {
      this.bagTypeOptions = { data: [], loaded: false, loading: false };
      this.compositeTypeOptions = { data: [], loaded: false, loading: false };
      this.materialOptions = { data: [], loaded: false, loading: false };
      this.sizeOptions = { data: [], loaded: false, loading: false };
      this.surfaceFeeOptions = { data: [], loaded: false, loading: false };
      this.accessoryOptions = { data: [], loaded: false, loading: false };
      this.rollOptions = { data: [], loaded: false, loading: false };
      this.bagTypeByType = {
        0: { data: [], loaded: false, loading: false },
        1: { data: [], loaded: false, loading: false },
        2: { data: [], loaded: false, loading: false },
      };
    },

    /**
     * 清除指定类型的缓存
     */
    clearCacheByType(type) {
      switch (type) {
        case "bagType":
          this.bagTypeOptions = { data: [], loaded: false, loading: false };
          this.bagTypeByType = {
            0: { data: [], loaded: false, loading: false },
            1: { data: [], loaded: false, loading: false },
            2: { data: [], loaded: false, loading: false },
          };
          break;
        case "compositeType":
          this.compositeTypeOptions = { data: [], loaded: false, loading: false };
          break;
        case "material":
          this.materialOptions = { data: [], loaded: false, loading: false };
          break;
        case "size":
          this.sizeOptions = { data: [], loaded: false, loading: false };
          break;
        case "surfaceFee":
          this.surfaceFeeOptions = { data: [], loaded: false, loading: false };
          break;
        case "accessory":
          this.accessoryOptions = { data: [], loaded: false, loading: false };
          break;
        case "roll":
          this.rollOptions = { data: [], loaded: false, loading: false };
          break;
      }
    },
  },
});
